package top.songxs.gmall.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.songxs.gmall.feign.search.GoodsFeignClient;
import top.songxs.gmall.product.dto.SkuInfoDto;
import top.songxs.gmall.product.entity.*;
import top.songxs.gmall.product.mapper.SkuAttrValueMapper;
import top.songxs.gmall.product.mapper.SkuInfoMapper;
import top.songxs.gmall.product.rpc.biz.CategoryFeignRpcService;
import top.songxs.gmall.product.service.*;
import top.songxs.gmall.product.view.CategoryView;
import top.songxs.gmall.search.entity.Goods;
import top.songxs.gmall.search.entity.SearchAttr;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * @author 86176
 * @description 针对表【sku_info(库存单元表)】的数据库操作Service实现
 * @createDate 2023-05-15 20:14:07
 */
@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo>
        implements SkuInfoService {
    @Autowired
    private SkuAttrValueService skuAttrValueService;
    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    private SkuImageService skuImageService;

    @Override
    public Page<SkuInfo> findSkuInfo(Integer pageNum, Integer pageSize) {
        Page<SkuInfo> skuInfoPage = new Page<>(pageNum, pageSize);
        this.page(skuInfoPage);
        return skuInfoPage;
    }

    @Override
    @Transactional
    public void saveSkuInfo(SkuInfoDto skuInfoDto) {
        SkuInfo skuInfo = new SkuInfo();
        BeanUtils.copyProperties(skuInfoDto, skuInfo);
        this.save(skuInfo);
        //这一步是skuAttrValue
        List<SkuAttrValue> skuAttrValueList = skuInfoDto.getSkuAttrValueList();
        skuAttrValueList = skuAttrValueList.stream().peek(skuAttrValue -> {
            skuAttrValue.setSkuId(skuInfo.getId());
        }).collect(Collectors.toList());
        skuAttrValueService.saveBatch(skuAttrValueList);

        //这一步是skuSaleAttrValue
        List<SkuSaleAttrValue> skuSaleAttrValueList = skuInfoDto.getSkuSaleAttrValueList();
        skuSaleAttrValueList = skuSaleAttrValueList.stream().peek(skuSaleAttrValue -> {
            skuSaleAttrValue.setSkuId(skuInfo.getId());
            skuSaleAttrValue.setSpuId(skuInfoDto.getSpuId());
        }).collect(Collectors.toList());
        skuSaleAttrValueService.saveBatch(skuSaleAttrValueList);

        //这一步是skuImg
        List<SkuImage> skuImageList = skuInfoDto.getSkuImageList();
        skuImageList = skuImageList.stream().peek(skuImage -> {
            skuImage.setSkuId(skuInfo.getId());
        }).collect(Collectors.toList());
        skuImageService.saveBatch(skuImageList);
    }

    @Autowired
    private GoodsFeignClient goodsFeignClient;

    @Override
    public void ongSale(Long skuId) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setIsSale(1);
        skuInfo.setId(skuId);
        this.updateById(skuInfo);
        Goods goods = this.buildGoods(skuId);
        goodsFeignClient.saveGoods(goods);
    }


    @Autowired
    private BaseTrademarkService baseTrademarkService;

    @Autowired
    private CategoryFeignRpcService categoryFeignRpcService;
    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;

    private Goods buildGoods(Long skuId) {
        Goods goods = new Goods();
        goods.setId(skuId);
        SkuInfo skuInfo = this.getById(skuId);
        //skuInfo的默认属性
        goods.setDefaultImg(skuInfo.getSkuDefaultImg());
        goods.setTitle(skuInfo.getSkuName());
        goods.setPrice(skuInfo.getPrice());
        goods.setCreateTime(new Date());

        CompletableFuture<Void> cf1 = CompletableFuture.runAsync(() -> {
            //设置品牌的属性值
            BaseTrademark trademark = baseTrademarkService.getById(skuInfo.getTmId());
            goods.setTmId(skuInfo.getTmId());
            goods.setTmName(trademark.getTmName());
            goods.setTmLogoUrl(trademark.getLogoUrl());
        });

        CompletableFuture<Void> cf2 = CompletableFuture.runAsync(() -> {
            CategoryView allCategory = categoryFeignRpcService.findAllCategory(skuId);
            //设置根据skuId查询到的三级分类
            goods.setCategory1Id(allCategory.getCategory1Id());
            goods.setCategory1Name(allCategory.getCategory1Name());
            goods.setCategory2Id(allCategory.getCategory2Id());
            goods.setCategory2Name(allCategory.getCategory2Name());
            goods.setCategory3Id(allCategory.getCategory3Id());
            goods.setCategory3Name(allCategory.getCategory3Name());
        });
        CompletableFuture<Void> cf3 = CompletableFuture.runAsync(() -> {
            //设置SearchAttr集合
            List<SearchAttr> attrs = skuAttrValueMapper.findAttrsBySkuId(skuId);
            goods.setAttrs(attrs);
        });
        //设置热点分数
        goods.setHotScore(0L);
        CompletableFuture.allOf(cf1, cf2, cf3).join();
        return goods;

    }

    @Override
    public void cancelSale(Long skuId) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setIsSale(0);
        skuInfo.setId(skuId);
        this.updateById(skuInfo);
        goodsFeignClient.deleteGoods(skuId);
    }
}




